package cn.pet.service.manage.controller;


import cn.pet.service.manage.entity.PetSubscribeType;
import cn.pet.service.manage.entity.api.Result;
import cn.pet.service.manage.entity.dto.SubscribeTypeDTO;
import cn.pet.service.manage.service.PetSubscribeTypeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 预约类型 前端控制器
 * </p>
 *
 * @author
 * @since 2023-02-11
 */
@RestController
@RequestMapping("/manage/petSubscribeType")
public class PetSubscribeTypeController {

    @Resource
    private PetSubscribeTypeService petSubscribeTypeService;

    /**
     * 预约服务类型列表
     * @return
     */
    @GetMapping("list")
    public Object list(SubscribeTypeDTO subscribeTypeDTO){
        //构造分页构造器
        PageHelper.startPage(subscribeTypeDTO.getPageNum(),subscribeTypeDTO.getPageSize());
//        Page pageInfo = new Page(subscribeTypeDTO.getPage(),subscribeTypeDTO.getPageSize());
        //构造条件构造器
        LambdaQueryWrapper<PetSubscribeType> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(subscribeTypeDTO.getName()),PetSubscribeType::getName,subscribeTypeDTO.getName());
        //添加排序条件
        queryWrapper.orderByDesc(PetSubscribeType::getCreateTime);
        //执行查询
        List<PetSubscribeType> list = petSubscribeTypeService.list(queryWrapper);
//        petSubscribeTypeService.page(pageInfo, queryWrapper);
        return Result.ok("success",new PageInfo<>(list));
    }

    /**
     * 添加商品类型
     * @param petSubscribeType
     * @return
     */
    @PostMapping("add")
    public Object add(@RequestBody PetSubscribeType petSubscribeType){
        if (!petSubscribeTypeService.save(petSubscribeType)){
            return Result.error("添加失败");
        }
        return Result.ok("添加成功");
    }

    /**
     * 编辑类型
     * @param petSubscribeType
     * @return
     */
    @PutMapping("edit")
    public Object edit(@RequestBody PetSubscribeType petSubscribeType){
        if (!petSubscribeTypeService.updateById(petSubscribeType)){
            return Result.error("编辑失败");
        }
        return Result.ok("编辑成功");
    }

    /**
     * 查看类型信息
     * @param id
     * @return
     */
    @GetMapping("detail/{id}")
    public Object detail(@PathVariable("id") Integer id){
        return Result.ok("success",petSubscribeTypeService.getById(id));
    }

    /**
     * 删除类型
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public Object delete(@PathVariable("id") Integer id){
        if (!petSubscribeTypeService.removeById(id)){
            return Result.error("删除失败");
        }
        return Result.ok("删除成功");
    }

    /**
     * 获取预约服务下拉列表
     * @return
     */
    @GetMapping("getList")
    public Object getList(){
        return Result.ok("success",petSubscribeTypeService.list());
    }

}
